﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.IO.IsolatedStorage;

namespace UI
{
    public class APIInfo
    {
        public APIInfo(string url, string method)
        {
            this.APIUrl = url;
            this.APIMethod = method;
        }

        public string APIUrl { get; private set; }
        public string APIMethod { get; private set; }
    }

    public class ConfigHelper
    {
        static ConfigHelper()
        {
            XDocument config = XDocument.Load("Config.xml");
            XElement root = config.Root;
            ConfigHelper.AppKey = root.Element("AppKey").Value;
            ConfigHelper.AppSecret = root.Element("AppSecret").Value;
            ConfigHelper.RequestMillisecondsTimeout = int.Parse(root.Element("RequestMillisecondsTimeout").Value);
            ConfigHelper.CertificateUrl = root.Element("CertificateUrl").Value;
            ConfigHelper.AuthorizeAPI = new APIInfo(root.Element("AuthorizeAPI").Value, root.Element("AuthorizeAPI").Attribute("Method").Value);
            ConfigHelper.GetUIDAPI = new APIInfo(root.Element("GetUIDAPI").Value, root.Element("GetUIDAPI").Attribute("Method").Value);
            ConfigHelper.TimelineAPI = new APIInfo(root.Element("TimelineAPI").Value, root.Element("TimelineAPI").Attribute("Method").Value);
            ConfigHelper.UserInfoAPI = new APIInfo(root.Element("UserInfoAPI").Value, root.Element("UserInfoAPI").Attribute("Method").Value);
            ConfigHelper.DeleteTweetAPI = new APIInfo(root.Element("DeleteTweetAPI").Value, root.Element("DeleteTweetAPI").Attribute("Method").Value);
            ConfigHelper.PublishTweetAPI = new APIInfo(root.Element("PublishTweetAPI").Value, root.Element("PublishTweetAPI").Attribute("Method").Value);
            ConfigHelper.PublishRetweetAPI = new APIInfo(root.Element("PublishRetweetAPI").Value, root.Element("PublishRetweetAPI").Attribute("Method").Value);
            ConfigHelper.PublishTweetWithImageAPI = new APIInfo(root.Element("PublishTweetWithImageAPI").Value, root.Element("PublishTweetWithImageAPI").Attribute("Method").Value);
            ConfigHelper.GetFollowingAPI = new APIInfo(root.Element("GetFollowingAPI").Value, root.Element("GetFollowingAPI").Attribute("Method").Value);
            ConfigHelper.UnfollowAPI = new APIInfo(root.Element("UnfollowAPI").Value, root.Element("UnfollowAPI").Attribute("Method").Value);
            ConfigHelper.FollowAPI = new APIInfo(root.Element("FollowAPI").Value, root.Element("FollowAPI").Attribute("Method").Value);
        }

        public const string AccessTokenString = "AccessToken";
        public const string AccessTokenExpiredTimeString = "AccessTokenExpiredTime";
        public const string UserIdString = "UserId";
        public const string IsValidSettingsString = "IsValidSettings";

        public static APIInfo TimelineAPI { get; private set; }

        public static APIInfo GetUIDAPI { get; private set; }

        public static string AppKey { get; private set; }

        public static string AppSecret { get; private set; }

        public static int RequestMillisecondsTimeout { get; private set; }

        public static string CertificateUrl { get; private set; }

        public static APIInfo AuthorizeAPI { get; private set; }

        public static APIInfo UserInfoAPI { get; private set; }

        public static APIInfo DeleteTweetAPI { get; private set; }

        public static APIInfo PublishTweetAPI { get; private set; }

        public static APIInfo PublishRetweetAPI { get; private set; }

        public static APIInfo PublishTweetWithImageAPI { get; private set; }

        public static APIInfo GetFollowingAPI { get; private set; }

        public static APIInfo UnfollowAPI { get; private set; }

        public static APIInfo FollowAPI { get; private set; }

        public static string AccessToken
        {
            get
            {
                return (string)IsolatedStorageSettings.ApplicationSettings[ConfigHelper.AccessTokenString];
            }
            set
            {
                IsolatedStorageSettings.ApplicationSettings[ConfigHelper.AccessTokenString] = value;
            }
        }

        public static DateTimeOffset AccessTokenExpiredTime
        {
            get
            {
                return (DateTimeOffset)IsolatedStorageSettings.ApplicationSettings[ConfigHelper.AccessTokenExpiredTimeString];
            }
            set
            {
                IsolatedStorageSettings.ApplicationSettings[ConfigHelper.AccessTokenExpiredTimeString] = value;
            }
        }

        public static string UserId
        {
            get
            {
                return (string)IsolatedStorageSettings.ApplicationSettings[ConfigHelper.UserIdString];
            }
            set
            {
                IsolatedStorageSettings.ApplicationSettings[ConfigHelper.UserIdString] = value;
            }
        }

        public static bool IsValidSettings
        {
            get
            {
                return (bool)IsolatedStorageSettings.ApplicationSettings[ConfigHelper.IsValidSettingsString];
            }
            set
            {
                IsolatedStorageSettings.ApplicationSettings[ConfigHelper.IsValidSettingsString] = value;
            }
        }
    }
}
